package com.codingvip.sort;

public class QuickSort {
    public static void main(String[] args) {
//        int[] arr = {10, 7, 2, 4, 7, 62, 3, 4, 2, 1, 8, 9, 19};
        int[] arr = {10, 7, 2, 4, 7, 62};
        QSort(arr, 0, arr.length - 1);

        myPrint(arr);
    }

    public static int Partition(int[] a, int low, int high) {
        int point;
        point = a[low];
        while (low < high) {
            while (low < high && a[high] >= point) {
                high--;
            }
            swap(a, low, high);
            while (low < high && a[low] <= point) {
                low++;
            }
            swap(a, low, high);

        }
        return low;
    }

    public static void QSort(int[] k, int low, int high) {
        int point;

        if (low < high)
        {
            point = Partition(k, low, high);

            QSort(k, low, point - 1);

            QSort(k, point + 1, high);
        }
    }

    private static void swap(int[] k, int low, int high) {
        int temp;

        temp = k[low];
        k[low] = k[high];
        k[high] = temp;
    }

    private static void myPrint(int[] arr) {

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "- ");
        }
        System.out.println();
    }

}
